/**
 * This plugin creates toolbars for the quick components insert (html tags, php operators, sql comands etc)
 */  
var EditArea_tags = 
{
	/**
	 * Get called once this file is loaded (editArea still not initialized)
	 *
	 * @return nothing	 
	 */	 	 	
	init: function()
	{	
		editArea.load_css(this.baseURL+"css/tags.css");
		//editArea.load_script(this.baseURL+"test2.js");
	}
	
	/**
	 * Returns the HTML code for a specific control string or false if this plugin doesn't have that control.
	 * A control can be a button, select list or any other HTML item to present in the EditArea user interface.
	 * Language variables such as {$lang_somekey} will also be replaced with contents from
	 * the language packs.
	 * 
	 * @param {string} ctrl_name: the name of the control to add	  
	 * @return HTML code for a specific control or false.
	 * @type string	or boolean
	 */
	,get_control_html: function(ctrl_name)
	{
		switch(ctrl_name)
		{
			case "insert_button":
				// Control id, button img, command
				return parent.editAreaLoader.get_button_html('insert_button', 'insert.png', 'insert_cmd', false, this.baseURL);
			case "tag_select":
				syntax_selection_element = document.getElementById('syntax_selection');
				current_syntax = syntax_selection_element.options[syntax_selection_element.selectedIndex].text.toLowerCase();
				
				//prints tag selection based on the selected syntax
				html= "<select id='tag_select' onchange='javascript:editArea.execCommand(\"insert_construction\")' fileSpecific='no'>"
				+"			<option value='-1'>Select...</option>"

				switch (current_syntax)
				{
					case "html": 
						html += "	<option value='<h1>text</h1>'>h1</option>"
						+"			<option value='<h2>text</h2>'>h2</option>"
						+"			<option value='<h3>text</h3>'>h3</option>"
						+"			<option value='<h4>text</h3>'>h4</option>"
						+"			<option value='<h5>text</h4>'>h5</option>"
						+"			<option value='<h6>text</h5>'>h6</option>"
						break;
						
					case 'php':
						html += "		<option value='if () \n{\n\ttext\n}\n'>if</option>"
							+"			<option value='foreach ($elements as $key=>$value) \n{\n\ttext\n}\n'>foreach</option>"
							+"			<option value='switch (text) \n{\n\tcase \"\": break;\n\tcase \"\": break;\n\tdefault:\n}\n'>switch/case</option>"
							+"			<option value='/*\n * {$comment_here}\n*/\nfunction func_name() \n{\n\ttext\n}\n'>function</option>"
						break;

					case 'spherusphp':
						html += "		<option value='$this->helpers->htmlx->InputTextRow(LANG_, \"text\", $this->data[$this->name][text], \"\");'>InputTextRow</option>"
						break;
						
					default:
						break;									
				}
				html += "		</select>";
				return html; 
		}
		return false;
	}
	
	/**
	 * Get called once EditArea is fully loaded and initialised
	 *	 
	 * @return nothing
	 */	 	 	
	,onload: function()
	{ 
		
	}
	
	/**
	 * Is called each time the user touch a keyboard key.
	 *	 
	 * @param (event) e: the keydown event
	 * @return true - pass to next handler in chain, false - stop chain execution
	 * @type boolean	 
	 */
	,onkeydown: function(e)
	{
/*		var str= String.fromCharCode(e.keyCode);
		// desactivate the "f" character
		if(str.toLowerCase()=="f")
		{
			return true;
		}
		return false;*/
	}
	
	/**
	 * Executes a specific command, this function handles plugin commands.
	 *
	 * @param {string} cmd: the name of the command being executed
	 * @param {unknown} param: the parameter of the command	 
	 * @return true - pass to next handler in chain, false - stop chain execution
	 * @type boolean	
	 */
	,execCommand: function(cmd, param)
	{
		// Handle commands
		switch(cmd)
		{
			case "tag_select_change":
				var val= document.getElementById("tag_select").value;
				if (val!=-1)
				{
					this._insertConstruction(editArea.id, val);
//					parent.editAreaLoader.insertTags(editArea.id, "<"+val+">", "</"+val+">");
				}
				document.getElementById("tag_select").options[0].selected=true; 
				return false;
				
			case "insert_construction":
				editArea.execCommand("tag_select_change");
				return false;
		}
		
		// Pass to next handler in chain
		return true;
	}
	
	/**
	 * This is just an internal plugin method, prefix all internal methods with a _ character.
	 * The prefix is needed so they doesn't collide with future EditArea callback functions.
	 *
	 * @param {string} a Some arg1.
	 * @param {string} b Some arg2.
	 * @return Some return.
	 * @type unknown
	 */
	,_insertConstruction: function(id, construction)
	{
		position = parent.editAreaLoader.getSelectionRange(id);
		x_position = document.getElementById("currPos").innerHTML;
		
		outdent = '\n';
		for (i=0; i < x_position-1; i++)
		{
			outdent = outdent + '\t';
		}
		construction = construction.replace(/\n/g, outdent);
		
    	var old_sel = parent.editAreaLoader.getSelectionRange(id);
    	if (parent.editAreaLoader.getSelectedText(id).length != 0)
    		text = construction.replace(/text/g, parent.editAreaLoader.getSelectedText(id));
    	else
    		text = construction.replace(/text/g, '');
    		
//    	text = open_tag + parent.editAreaLoader.getSelectedText(id) + close_tag; 
		parent.editAreaLoader.setSelectedText(id, text);
    	var new_sel= parent.editAreaLoader.getSelectionRange(id);
    	if(old_sel["end"] > old_sel["start"])	// if text was selected, cursor at the end
    		parent.editAreaLoader.setSelectionRange(id, new_sel["end"], new_sel["end"]);
 //   	else // cursor in the middle
 //   		parent.editAreaLoader.setSelectionRange(id, old_sel["start"]+open_tag.length, old_sel["start"]+open_tag.length);
	}
};

// Adds the plugin class to the list of available EditArea plugins
editArea.add_plugin("tags", EditArea_tags);